package com.road.vast.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @Author 郭泽鹏
 * @Date 2020/8/20 15:25
 * @Version 1.0
 * @Description
 */
@RestController
@RequestMapping("/send/")
public class MailController {

    @Autowired
    JavaMailSender mailSender;

    //发送者
    @Value("${spring.mail.username}")
    String from;

    /**
     * 发送一个简单格式的文本
     * @param to 接收者
     * @param subject 文本标题
     * @param text 文本内容
     * @return 发送结果
     */
    @GetMapping("simple_text/{to}/{subject}/{text}")
    public Boolean sendSimpleText(@PathVariable String to,
                            @PathVariable String subject,
                            @PathVariable String text){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);
        return Boolean.TRUE;
    }

    /**
     * 发送一个HTML格式的文本
     * @param to 接收者
     * @param subject 文本标题
     * @param text 文本内容
     * @return 发送结果
     */
    @GetMapping("html/{to}/{subject}/{text}")
    public Boolean sendHtml(@PathVariable String to,
                                  @PathVariable String subject,
                                  @PathVariable String text){

        MimeMessage message=mailSender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper=new MimeMessageHelper(message,true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text,true);      //是否是html格式的邮件
            mailSender.send(message);
        }catch (Exception e){
        }
        return Boolean.TRUE;
    }

    /**
     * 发送一个文档文件
     * @param to 接收者
     * @param subject 文本标题
     * @param text 文本内容
     * @return 发送结果
     */
    @GetMapping("doc/{to}/{subject}/{text}")
    public Boolean sendDoc(@PathVariable String to,
                           @PathVariable String subject,
                           @PathVariable String text,
                           @RequestParam(value = "file") MultipartFile file
                           ){

        MimeMessage message=mailSender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper=new MimeMessageHelper(message,true);

            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text);
            helper.addAttachment(file.getOriginalFilename(), file);

            mailSender.send(message);
        }catch (Exception e){
        }
        return Boolean.TRUE;
    }

    @GetMapping("inline/{to}/{subject}/{text}")
    public Boolean sendInlineMail(@PathVariable String to,
                               @PathVariable String subject,
                               @PathVariable String text) {
        MimeMessage mimeMailMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMailMessage, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);
            //文件路径
            FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/images/柯基犬.jpg"));
            helper.addInline(file.getFilename(), file);
            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
        }
        return Boolean.TRUE;
    }
}
